﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DotNetWikiBot;
using System.Threading;

namespace AlexTranslator
{
    public partial class LinksList : WikiWinForm
    {
        //public List<string> links = new List<string>();
        string mainArticleTitle = "";

        protected  LinksList()
        {
            InitializeComponent();
        }

        public LinksList(PageList pages, string mainArticleTitle)
        {
            
            
            InitializeComponent();

            foreach (Page p in pages)
            {
                p.title = p.title.Trim();
            }

            pages.RemoveRecurring();

            foreach (Page p in pages)
            {
                txbLinks.Text += p.title + "\r\n";
            }
            this.mainArticleTitle = mainArticleTitle;
        }

        public LinksList(List<string> links)
        {
            InitializeComponent();
            if (links.Count > 0)
            {
                txbLinks.Text = links.Aggregate((i, j) => i + "\r\n" + j);
            }
        }

        private void btnRedLinks_Click(object sender, EventArgs e)
        {
            var linksArr = new List<string>(txbLinks.Text.Split(new char[] { '\n', '\r', '=' }, StringSplitOptions.RemoveEmptyEntries));
            
            linksArr.RemoveAt(0); // we eliminate line "=====7 links==="
            linksArr = new List<string>(linksArr.Select((elem, i) => elem.Trim()).Where(n => (n != " ")));


            PageList pl = new PageList(UkSite, linksArr.ToArray());

            pl.LoadEx();

            //List<string> redLinksArr = new List<string>();

            txbLinks.Text = "";
            foreach (string link in linksArr)
            {
                if (!pl.Contains(link) && !link.Contains("Категорія"))
                {
                    txbLinks.Text += link + "\r\n";
                }
            }
        }

        private void btnProcessRedLinks_Click(object sender, EventArgs e)
        {
            ThreadPool.QueueUserWorkItem(
                new WaitCallback(ProcessRedLinks));
        }

        private PageList FillFoundList(string articleTitle)
        {
            Page p = new Page(UkSite, articleTitle);

            try
            {
                p.Load();
            }
            catch (WikiBotException ex)
            {
                if (!ex.Message.Contains("login"))
                    throw;
            }

            PageList fList = new PageList(UkSite);

            if (string.IsNullOrEmpty(p.text))
            {
                try
                {

                    fList.FillFromGoogleSearchResults(articleTitle, 10);

                    fList.Remove(mainArticleTitle);
                }
                catch (WikiBotException ex)
                {
                    if (!ex.Message.Contains("login"))
                        throw;
                }
                finally
                {

                }
            }
            return fList;
        }

        private void ProcessRedLinks(object o)
        {
            List<string> linksArr = null;
            this.Invoke(new MethodInvoker(delegate
                               {
                                   linksArr = new List<string>(txbLinks.Text.Split(new char[] { '\n', '\r', '=' }, StringSplitOptions.RemoveEmptyEntries));
                               }));

            //Split(new[] { ',' }, 2);
            linksArr.RemoveAt(0); // we eliminate line "=====7 links==="
            linksArr = new List<string>(linksArr.Select((elem, i) => elem.Trim()).Where(n => (n != " ")));

            //var d = from link in linksArr
            //        select new Page(link);

            PageList pl = new PageList(UkSite, linksArr.ToArray());

            pl.LoadEx();

            int counter=0;
            foreach (string link in linksArr)
            {
                counter++;
                if (!pl.Contains(link) && !link.Contains("Категорія"))
                {
                    PageList fList = FillFoundList(link.Trim());
                    if (fList.Count() > 0)
                    {
                        this.Invoke(new MethodInvoker(delegate
                                   {
                                       MatchRedLink f = new MatchRedLink(link,fList);
                                       f.Show();
                                   }));
                        
                    }
                }
                
            }
        }
    }
}
